package com.ruoyi.project.system.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysLuck;
import com.ruoyi.project.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysReceive;
import com.ruoyi.project.system.service.ISysReceiveService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 领取Controller
 *
 * @author theme
 * @date 2021-04-20
 */
@Api(description = "领取Controller" )
@RestController
@RequestMapping("/system/receive" )
public class SysReceiveController extends BaseController {
    @Autowired
    private ISysReceiveService sysReceiveService;

    @Autowired
    private ISysDictDataService sysDictDataService;


/**
 * 查询领取列表
 */
@ApiOperation("查询领取列表" )
//@PreAuthorize("@ss.hasPermi('system:receive:list')" )
@GetMapping("/list" )
        public TableDataInfo list(SysReceive sysReceive) {
        startPage();
        List<SysReceive> list = sysReceiveService.selectSysReceiveList(sysReceive);
        return getDataTable(list);
    }

    /**
     * 获取领取详细信息
     */
    @ApiOperation("重新发送立减金优惠券" )
    @PreAuthorize("@ss.hasPermi('system:receive:query')" )
    @GetMapping(value = "/{receiveId}" )
    public AjaxResult getInfo(@PathVariable("receiveId" ) Long receiveId) {
        SysReceive sysReceive=sysReceiveService.selectSysReceiveById(receiveId);
        AjaxResult result=sysReceiveService.valid(sysReceive,false);
        if(Integer.valueOf(result.get("code")+"")!= HttpStatus.SUCCESS){
            return result;
        }
        return toAjax(sysReceiveService.updateSysReceive(sysReceive));
    }

    /**
     * 新增领取
     */
    @ApiOperation("新增领取" )
    @PreAuthorize("@ss.hasPermi('system:receive:add')" )
    @Log(title = "领取" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysReceive sysReceive) {
        return toAjax(sysReceiveService.insertSysReceive(sysReceive));
    }

    /**
     * 修改领取
     */
    @ApiOperation("修改领取" )
    @PreAuthorize("@ss.hasPermi('system:receive:edit')" )
    @Log(title = "领取" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysReceive sysReceive) {
        return toAjax(sysReceiveService.updateSysReceive(sysReceive));
    }

    /**
     * 删除领取
     */
    @ApiOperation("删除领取" )
    @PreAuthorize("@ss.hasPermi('system:receive:remove')" )
    @Log(title = "领取" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{receiveIds}" )
    public AjaxResult remove(@PathVariable Long[] receiveIds) {
        return toAjax(sysReceiveService.deleteSysReceiveByIds(receiveIds));
    }

    @GetMapping("/export")
    public AjaxResult export(SysReceive sysReceive) {
        List<SysReceive> list = sysReceiveService.selectSysReceiveList(sysReceive);
        ExcelUtil<SysReceive> util = new ExcelUtil<SysReceive>(SysReceive.class);
        return util.exportExcel(list, "领取数据");
    }
}
